package com.demo.admin.util;

import org.apache.commons.lang3.StringUtils;
import com.demo.admin.domain.bo.AccessToken;
import com.demo.common.enums.GatewayAccountType;
import com.demo.common.util.ThreadContext;

/**
 * AdminRequestContext
 * 
 * @author demo
 * @data 2020-12-13 22:44:20
 */
public class AdminRequestContext {
    private static final String GATEWAY_ACCOUNT_ID_KEY = "GatewayAccountId";
    private static final String USERNAME_KEY = "Username";
    private static final String USER_TYPE_KEY = "UserType";

    private AdminRequestContext() {
        throw new IllegalStateException("Utility class");
    }

    public static void init(AccessToken accessToken) {
        Integer gatewayAccountId = accessToken.getGatewayAccountId();
        if (gatewayAccountId != null) {
            ThreadContext.put(GATEWAY_ACCOUNT_ID_KEY, gatewayAccountId);
        }
        String username = accessToken.getUsername();
        if (StringUtils.isNotBlank(username)) {
            ThreadContext.put(USERNAME_KEY, username);
        }
        String usertype = accessToken.getUsertype();
        if (StringUtils.isNotBlank(usertype)) {
            GatewayAccountType gatewayAccountType = GatewayAccountType.getByCode(usertype);
            ThreadContext.put(USER_TYPE_KEY, gatewayAccountType);
        }
    }

    public static Integer getGatewayAccountId() {
        return ThreadContext.getInteger(GATEWAY_ACCOUNT_ID_KEY);
    }

    public static String getUsername() {
        return ThreadContext.getString(USERNAME_KEY);
    }

    public static GatewayAccountType getUserType() {
        return (GatewayAccountType) ThreadContext.get(USER_TYPE_KEY);
    }

}
